package com.linzm.biz;

import com.linzm.jdbc.JdbcSingleton;
import com.linzm.jdbc.Utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Demo {
    public static void main(String[] args) throws SQLException {
        // 数据库连接配置
        String url = "jdbc:mysql://localhost:3306/esciku";
        String username = "root";
        String password = "root";

        Statement statement = null;
        ResultSet resultSet = null;
        // 获取JDBC连接实例
        JdbcSingleton jdbcSingleton = JdbcSingleton.getInstance();
        try {
            // 建立数据库连接
            Connection connection = jdbcSingleton.getConnection(url, username, password);

            // 执行SQL查询
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT id, keyword FROM extended_word WHERE flag=0");

            // 处理结果集
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String keyword = resultSet.getString("keyword");
                System.out.println("keyword: " + keyword + ", id: " + id);
            }

            // 关闭连接
            resultSet.close();
            statement.close();
            jdbcSingleton.closeConnection();
        } catch (SQLException exception) {
            exception.printStackTrace();
        } finally {
            jdbcSingleton.closeConnection();
            Utils.closeStatement(statement);
            Utils.closeResultSet(resultSet);
        }
    }
}
